package com.xinghou.platform.dao.wzk.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.ExposureDAO;
import com.xinghou.platform.dao.main.WzkExposureDAO;
import com.xinghou.platform.model.main.Exposure;
import com.xinghou.platform.server.mq.MqMessage;
/**
 * @author zhen.xu
 *
 */
@Service
public class WzkExposureDAOImpl implements WzkExposureDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	@Resource
	private MqMessage mqMessage;

	@Override
	public List<Exposure> getExposure(Timestamp num, int userid,int limit)
			throws DataAccessException {
		Object[] obj = new Object[]{userid,num,limit};
		String sql="SELECT * FROM wzk.exposure where STATUS=1 and exposure_userid=? and status>0 and updatetime<? ORDER BY updatetime DESC LIMIT ?";
		if(userid==-1)
		{
			sql="SELECT * FROM wzk.exposure where STATUS=1 and status>0 and updatetime<? ORDER BY updatetime DESC LIMIT ?";
			obj = new Object[]{num,limit};
		}
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		List<Exposure> exposureList=jdbcTemplate.query(sql, obj, new ExposureMapper());
		return exposureList;
	}
	@Override
	public Exposure getExposureByexpId(Integer expId)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM wzk.exposure where Exp_id = ? and status>0";
		Exposure exposure = jdbcTemplate.queryForObject(sql, new Object[] {expId}, new ExposureMapper());
		CustomerContextHolder.clearCustomerType();
		return exposure;
	}
	@Override
	public int updateExposure(Exposure exposure) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE wzk.exposure SET exposure_content = ? , exposure_userid = ? , exposure_type = ? , exposure_id = ? , exposure_id_type = ? , exposure_viewcount = ? , exposure_praisecount = ? , createtime = ? , updatetime = ?, STATUS = ? WHERE Exp_id = ? ;"	
				);
		Object[] params = {exposure.getExposureContent(),exposure.getExposureUserid(),exposure.getExposureType(),exposure.getExposureId(),exposure.getExposureIdType(),exposure.getExposureViewcount(),exposure.getExposurePraisecount(),exposure.getCreatetime(),exposure.getUpdatetime(),exposure.getStatus(),exposure.getExpId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		

		if(ret>0){
			mqMessage.sendMessageExposure(exposure.getExpId()+"",true);
		}
		
		return ret;
	}
	@Override
	public int saveExposure(Exposure exposure) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO exposure (exposure_content, exposure_userid, exposure_type, exposure_id, "
				+ "exposure_id_type, exposure_viewcount, exposure_praisecount, createtime,updatetime, STATUS)VALUES(?,?,?,?,?,?,?,?,?,?)");
		Object[] params = {exposure.getExposureContent(),exposure.getExposureUserid(),exposure.getExposureType(),exposure.getExposureId(),
				exposure.getExposureIdType(),exposure.getExposureViewcount(),exposure.getExposurePraisecount(),exposure.getCreatetime(),exposure.getUpdatetime(),exposure.getStatus()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		
		if(ret>0){
			mqMessage.sendMessageExposure(ret+"",false);
		}
		return ret;
	}
	
	private static final class ExposureMapper implements RowMapper<Exposure> {
		public Exposure mapRow(ResultSet rs, int rowNum) throws SQLException {
			Exposure exposure = new Exposure();
			exposure.setCreatetime(rs.getTimestamp("createtime"));
			exposure.setExpId(rs.getInt("Exp_id"));
			exposure.setExposureContent(rs.getString("exposure_content"));
			exposure.setExposureIdType(rs.getInt("exposure_id_type"));
			exposure.setExposureId(rs.getInt("exposure_id"));
			exposure.setExposurePraisecount(rs.getInt("exposure_praisecount"));
			exposure.setExposureType(rs.getInt("exposure_type"));
			exposure.setExposureUserid(rs.getInt("exposure_userid"));
			exposure.setExposureViewcount(rs.getInt("exposure_viewcount"));
			exposure.setStatus(rs.getShort("STATUS"));
			exposure.setUpdatetime(rs.getTimestamp("updatetime"));
			
			return exposure;
		}
	}

	@Override
	public List<Exposure> getExposureAll(int limit) throws DataAccessException {
		Object[] obj = new Object[]{limit};
		String sql="SELECT * FROM wzk.exposure where STATUS=1 order by Exp_id DESC limit ? ";
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		List<Exposure> exposureList=jdbcTemplate.query(sql, obj, new ExposureMapper());
		return exposureList;
	}
	
	@Override
	public List<Exposure> getExposureThreeDay(Timestamp bt,Timestamp et) throws DataAccessException {
		Object[] obj = new Object[]{bt,et};
		String sql="SELECT * FROM exposure where STATUS=1 and status>0 and updatetime>? AND updatetime<?";
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		List<Exposure> exposureList=jdbcTemplate.query(sql, obj, new ExposureMapper());
		return exposureList;
	}
}
